Skip to content

Several performance improvements - #49

Merged
sirbrillig merged 4 commits into
trunkfrom
make-perf-improvements
Mar 15, 2026
Merged

sirbrillig merged 4 commits into
trunkfrom
make-perf-improvements

Conversation

@sirbrillig

@sirbrillig sirbrillig commented Mar 15, 2026 •

Copy link
Copy Markdown
Owner

This implements several smaller performance improvements. The biggest of which is increasing the number of default threads.

  • Default number of threads to system amount
  • Fix performance bugs with does_file_match_query
  • Increase BufReader size
  • Only create Finder once

Stats

On a 4.0G codebase with no explicit threads and a cold cache on a M1 Macbook Pro.

Before:

$ hyperfine --prepare 'sync; sudo purge' 'grepdef WPCOM_JSON_API_New_User_Endpoint'
Benchmark 1: grepdef WPCOM_JSON_API_New_User_Endpoint
  Time (mean ± σ):      3.334 s ±  0.091 s    [User: 0.702 s, System: 5.166 s]
  Range (min … max):    3.164 s …  3.447 s    10 runs

After:

$ hyperfine --prepare 'sync; sudo purge' 'grepdef WPCOM_JSON_API_New_User_Endpoint'
Benchmark 1: grepdef WPCOM_JSON_API_New_User_Endpoint
  Time (mean ± σ):      2.634 s ±  0.091 s    [User: 0.918 s, System: 9.316 s]
  Range (min … max):    2.425 s …  2.783 s    10 runs

`does_file_match_query` had two bugs when using the `PrescanMemmem` search method:

1. `full.extend(buf)` always appended the full 2048-byte buffer regardless of how many bytes `file.read()` actually returned. On the last read of a file (or any short read), this fed stale bytes from a previous iteration into the search buffer, which could produce false negatives — causing the prescan to incorrectly skip files that contain the query.

2. The overlap logic between chunks split on newlines, which has nothing to do with ensuring cross-chunk matches are found. The correct approach is to retain exactly `query.len() - 1` bytes of overlap, so a match that starts near the end of one chunk and ends in the next is never missed.
`search_file_line_by_line` used the default 8 KB `BufReader` buffer, which means a syscall roughly every 8 KB when scanning large files. Increasing the buffer to 64 KB reduces the number of `read` syscalls by up to 8x on large files with no change in behavior.
`memmem::Finder::new` preprocesses the needle into an internal search table. Previously it was called inside `does_file_match_query`, rebuilding that table on every file scanned. This moves the construction into the outer closure of `build_parallel().run()`, so the `Finder` is built once per walk thread and reused for every file on that thread. For a search over thousands of files, this reduces `Finder::new` calls from O(files) to O(threads).
@sirbrillig
sirbrillig merged commit f5dd80e into trunk Mar 15, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant